<?php

/**
 * Convert this to a class.
 */

require_once('magpierss/rss_fetch.inc');

define('MAGPIE_CACHE_DIR', '/tmp/magpie_cache');
define('MAGPIE_CACHE_ON', TRUE);
define('MAGPIE_CACHE_AGE', 3600);

define('DEBUG_RSS', 0);

// different blog types that this script is setup to receive
define('BLOG_TYPE_MYSPACE', 0);
define('BLOG_TYPE_TRIBE', 1);
define('BLOG_TYPE_WORDPRESS', 2);
define('BLOG_TYPE_GOPETS', 3);
define('BLOG_TYPE_FRIENDSTER', 4);
define('BLOG_TYPE_MULTIPLY', 5);
define('BLOG_TYPE_LIVEJOURNAL', 6);

define('NUM_POSTS_VISIBLE', 3); // posts visible per page

// $url = 'http://rejon.org/wordpress/wp-feed.php?feed=rss2';
// $url = 'http://blog.myspace.com/blog/rss.cfm?friendID=22137457';
// $url = 'http://blog.myspace.com/blog/rss.cfm?friendID=10866659';
// $url = 'http://people.tribe.net/kidproto/blog/rss?r=10535';

//$blog_type = BLOG_TYPE_MYSPACE;
// $blog_type = BLOG_TYPE_TRIBE;



class FeedFormat
{
    /**
     * @var string               url to the feed
     */
    var $_url;

    /**
     * @var number               defined constant of type of blog
     */
    var $_blog_type;

    /**
     * @var number               first number in the stack of all posts
     */
    var $_start_num;

    /**
     * @var number               num of posts visible on each page
     */
    var $_num_posts_visible;

    /**
     * @var string               name of var for URL
     */
    var $_start_num_name;

    /**
     * @var string               name of var for URL
     */
    var $_increment_name;

    /**
     * @var bool                 true to print header, or default, false
     */
    var $_print_header;

    /**
     * @var object               magpierss object
     */
    var $_rss;

    /**
    * Prints a standard formatted blog with previous and more controls, 
    * along with the link to the rss feed. You can style it with css. You 
    * can use different $start_num_name and $increment_name and make sure
    * to pass into this function the right $start_num and 
    * $num_posts_visible so that the passing of that name to the URL is 
    * not confused. This allows for multiple blog feeds to be per page.
    *
    * @param string $url               url to the feed
    * @param number $blog_type         defined constant of type of blog
    * @param number $start_num         first num in the stack of all posts
    * @param number $num_posts_visible num of posts visible on each page
    * @param string $start_num_name    name of var for URL
    * @param string $increment_name    name of var for URL
    * @param bool   $print_header      true for header, or default, false
    */
    function FeedFormat ($url, $blog_type, $start_num, 
                         $num_posts_visible = NUM_POSTS_VISIBLE,
                         $start_num_name = 'start_num',
                         $increment_name = 'inc',
                         $print_header   = FALSE )
    {
        $this->_url               = $url;
        $this->_blog_type         = $blog_type;
        $this->_start_num         = $start_num;
        $this->_num_posts_visible = $num_posts_visible;
        $this->_start_num_name    = $start_num_name;
        $this->_increment_name    = $increment_name;
        $this->_print_header      = $print_header;

        $this->fetchFeed();
    }

    /**
     * Get latest version of the data feed
     */
    function fetchFeed ()
    {
        $this->_rss               = fetch_rss($this->_url);
    }


    /**
     * Prints the blog to the web
     */
    function toWeb ()
    {
        $num_posts_total = count($this->_rss->items); // total num of posts

        // check the start_num var passed in from URL
        if ( isset($this->_start_num) && 
            $this->_start_num >= 0 && 
            $this->_start_num <= $num_posts_total )
            $this->_start_num = $this->_start_num;
        else
            $this->_start_num = 0;

        if ( $this->_num_posts_visible < 1 )
            $this->_num_posts_visible = NUM_POSTS_VISIBLE;

        // if there is less total than what you want to view
        if ( $this->_num_posts_visible > $num_posts_total )
            $this->_num_posts_visible = $num_posts_total;

        echo "<div class=\"blog\">";

        if ( DEBUG_RSS || $this->_print_header ) {
            echo sprintf('<h3 class="title"><a href="%s">%s</a></h4>', 
                        $this->_rss->channel['link'],
                        $this->_rss->channel['title']);
        }

        $ct = 0; // set here to manifest scoping
        for ( $ct = $this->_start_num; 
              $ct < $this->_num_posts_visible+$this->_start_num; 
              $ct++ )
        {

            // if the $ct is not valid, then bust out of loop and stop!
            if ( $this->_num_posts_visible+$this->_start_num <= $ct || 
                 !isset($this->_rss->items[$ct]) )
                break;

            $item = &$this->_rss->items[$ct]; // make it a ref

            $text        = '';
            $comments    = '';
            $link        = '';
            $description = '';
            $read_more   = '';
            $pubdate     = '';
            $author      = '';

            $caption     = '';

            $date_fmt    = 'Y-m-d H:m';

            // per blog type settings
            switch ($this->_blog_type) {

                case BLOG_TYPE_MYSPACE:
                    $text = $item['summary'];
                    if ( strlen($text) >= 203 )
                    $read_more = 
                        '<a href="' . $item['link'] .'">read more</a>';
                    $pubdate = date($date_fmt, $item['date_timestamp']);
                    $caption = "Posted on $pubdate";
                    break;

                case BLOG_TYPE_TRIBE:
                    $comments = 
                        '<a href="' . $item['link'] . '">comments</a>';
                    break;

                case BLOG_TYPE_MULTIPLY:
                    $pubdate = date($date_fmt, $item['date_timestamp']);
                    $comments = 
                        '<a href="' . $item['guid'] . '">comments</a>';
                    $link = $item['guid'];
                    break;

                case BLOG_TYPE_LIVEJOURNAL:
                    $pubdate = date($date_fmt, $item['date_timestamp']);
                    $text = $item['summary'];
                    break;

                case BLOG_TYPE_FRIENDSTER:
                    $pubdate = date($date_fmt, $item['date_timestamp']);
                    $text = $item['summary'];
                    if ( strlen($text) >= 217 )
                        $read_more = 
                            '<a href="' . $item['link'] .'">read more</a>';
                    $comments = 
                        '<a href="' . $item['link'] . '">comments</a>';
                    break;

                case BLOG_TYPE_GOPETS:
                    // $caption = "Posted on $pubdate";
                    $pubdate = $item['pubdate'];
                    // continue
                    break;

                case BLOG_TYPE_WORDPRESS:
                    $text  = $item['content']['encoded'];
                    $pubdate = date($date_fmt, 
                                    strtotime($item['pubdate']));
                    break;
            }

            if ( empty($author) && isset($item['dc']['creator']) )
                $author  = $item['dc']['creator'];

            if ( empty($comments) && !empty($item['comments']) )
                $comments = 
                    '<a href="' . $item['comments'] . '">comments</a>';

            // making the lovely caption for the end of the post
            if (!empty($pubdate) && empty($caption) ) {
                $caption = "Posted on $pubdate";
            }

            if ( !empty($author) && !empty($caption) )
                $caption .= " by $author";
            else if ( !empty($author) )
                $caption .= "Posted by $author";

            if ( !empty($caption) )
                $caption .= " - $comments";
            else
                $caption = $comments;

            if ( !empty($read_more) )
                $caption = " - $caption";

            if ( empty($text) )
                $text = $item['summary'];

            $title = $item['title'];
            if ( empty($link) )
                $link   = $item['link'];
            $description = $item['description'];

            echo '<div class="item">' . 
                sprintf('<h4><a href="%s">%s</a></h4> %s %s %s</div>',
                        $link, $title, $text, $read_more, $caption );

            if ( DEBUG_RSS ) {
                echo "<p>text length: " . strlen($text) . "</p>";
            }
        }

        if ( $this->_num_posts_visible != NUM_POSTS_VISIBLE )
            $this->_increment_text = "&amp;" . 
                $this->_increment_name . "=" . $this->_num_posts_visible;
        else
            $this->_increment_text = '';

        $nav_text = '';
        // print out "previous posts" text
        if ( $ct - $this->_num_posts_visible ) {
            if ( $this->_start_num - $this->_num_posts_visible > 0 )
                $prev_ct = $this->_start_num - $this->_num_posts_visible;
            else
                $prev_ct = 0;

            $nav_text .= sprintf(' <a href="?%s=%d%s">&lt;&lt;</a>',
                                $this->_start_num_name, $prev_ct, 
                                $this->_increment_text );
        }

        // here print out how many entries are here

        // add space if previous text
        $spacer = !empty($nav_text) ? " " : "";

        $nav_text .= sprintf("%sposts %d - %d of %d ", $spacer,
                             $this->_start_num+1, 
                             $ct, $num_posts_total );

        $nav_text .= "from <a href=\"$url\">rss feed</a> ";

        // print out "more posts" text
        if ( $ct < $num_posts_total ) {
            $nav_text .= sprintf(' <a href="?%s=%d%s">&gt;&gt;</a>', 
                                $this->_start_num_name, $ct,
                                $this->_increment_text );
        }

        if ( !empty($nav_text) )
            echo "<p class=\"nav\" style=\"text-align:center\">" . 
                $nav_text . "</p>";

        echo "</div>"; // close out blog

        if ( DEBUG_RSS ) {

            echo '<hr />';
            echo "ct: $ct, num_posts_total: " . $this->_num_posts_total,
                " num_posts_visible: " . $this->_num_posts_visible ;
            echo  '<hr /><pre>';
            print_r( $this->_rss );
            echo '</pre>';
        }

    }   // end of printBlogFromFeed()
} // end of class Blog



/**
 * Prints a standard formatted blog with previous and more controls, 
 * along with the link to the rss feed. You can style it with css. You 
 * can use different $start_num_name and $increment_name and make sure
 * to pass into this function the right $start_num and $num_posts_visible
 * so that the passing of that name to the URL is not confused. This 
 * allows for multiple blog feeds to be per page.
 *
 * @param string $url               url to the feed
 * @param number $blog_type         defined constant of type of blog
 * @param number $start_num         first number in the stack of all posts
 * @param number $num_posts_visible num of posts visible on each page
 * @param string $start_num_name    name of var for URL
 * @param string $increment_name    name of var for URL
 * @param bool   $print_header      true to print header, or default, false
 */
function print_blog_from_feed ($url, $blog_type, $start_num, 
                              $num_posts_visible = NUM_POSTS_VISIBLE,
                              $start_num_name = 'start_num',
                              $increment_name = 'inc',
                              $print_header   = FALSE )
{
    $rss = fetch_rss($url);

    $num_posts_total = count($rss->items); // total num of items in the feed

    // check the start_num var passed in from URL
    if ( isset($start_num) && 
        $start_num >= 0 && 
        $start_num <= $num_posts_total )
        $start_num = $start_num;
    else
        $start_num = 0;

    if ( $num_posts_visible < 1 )
        $num_posts_visible = NUM_POSTS_VISIBLE;

    // if there is less total than what you want to view
    if ( $num_posts_visible > $num_posts_total )
        $num_posts_visible = $num_posts_total;

    echo "<div class=\"blog\">";

    if ( DEBUG_RSS || $print_header ) {
        echo sprintf('<h3 class="title"><a href="%s">%s</a></h4>', 
                     $rss->channel['link'], $rss->channel['title']);
    }

    $ct = 0; // set here to manifest scoping
    for ($ct = $start_num; $ct < $num_posts_visible+$start_num; $ct++) 
    {

        // if the $ct is not valid, then bust out of loop and stop wasting time!
        if ( $num_posts_visible+$start_num <= $ct || !isset($rss->items[$ct]) )
            break;

        $item = &$rss->items[$ct]; // make it a ref

        $text        = '';
        $comments    = '';
        $link        = '';
        $description = '';
        $read_more   = '';
        $pubdate     = '';
        $author      = '';

        $caption     = '';

        $date_fmt    = 'Y-m-d H:m';

        // per blog type settings
        switch ($blog_type) {
            case BLOG_TYPE_MYSPACE:
            $text = $item['summary'];
            if ( strlen($text) >= 203 )
               $read_more = '<a href="' . $item['link'] .'">read more</a>';

            $comments = '<a href="' . $item['comments'] . '">comments</a>';

            $pubdate = date($date_fmt, $item['date_timestamp']);
            $caption = "Posted on $pubdate";

            break;

       case BLOG_TYPE_TRIBE:
            $comments = '<a href="' . $item['link'] . '">comments</a>';
            break;

       case BLOG_TYPE_FRIENDSTER:
            $pubdate = date($date_fmt, $item['date_timestamp']);
            $text = $item['summary'];
            if ( strlen($text) >= 203 )
               $read_more = '<a href="' . $item['link'] .'">read more</a>';

       case BLOG_TYPE_GOPETS:
            // $caption = "Posted on $pubdate";
            $pubdate = $item['pubdate'];
            // continue
    
       case BLOG_TYPE_WORDPRESS:
            if ( isset($item['content']['encoded']) )
                $text  = $item['content']['encoded'];
            if ( isset($item['pubdate']) && !isset($pubdate) )
                $pubdate = date($date_fmt, strtotime($item['pubdate']));
            $author  = $item['dc']['creator'];
            $comments = '<a href="' . $item['comments'] . '">comments</a>';
            break;

            default:
        }

        // making the lovely caption for the end of the post
        if (!empty($pubdate) && empty($caption) ) {
            $caption = "Posted on $pubdate";
        }

        if ( !empty($author) && !empty($caption) )
            $caption .= " by $author";
        else if ( !empty($author) )
            $caption .= "Posted by $author";

        if ( !empty($caption) )
            $caption .= " - $comments";
        else
            $caption = $comments;

        if ( !empty($read_more) )
            $caption = " - $caption";

        if ( empty($text) )
            $text = $item['summary'];

        $title = $item['title'];
        $link   = $item['link'];
        $description = $item['description'];

        echo '<div class="item">' . 
             sprintf('<h4><a href="%s">%s</a></h4> %s %s %s</div>',
                     $link, $title, $text, $read_more, $caption );

        if ( DEBUG_RSS ) {
            echo "<p>text length: " . strlen($text) . "</p>";

        }

    }

    if ( $num_posts_visible != NUM_POSTS_VISIBLE )
        $increment_text = "&amp;$increment_name=$num_posts_visible";
    else
        $increment_text = '';

    $nav_text = '';
    // print out "previous posts" text
    if ( $ct - $num_posts_visible ) {
        if ( $start_num - $num_posts_visible > 0 )
            $prev_ct = $start_num - $num_posts_visible;
        else
            $prev_ct = 0;

        $nav_text .= sprintf(' <a href="?%s=%d%s">&lt;&lt;</a>',
                             $start_num_name, $prev_ct, $increment_text );
    }

    // here print out how many entries are here

    // add space if previous text
    $spacer = !empty($nav_text) ? " " : "";

    $nav_text .= sprintf("%sposts %d - %d of %d ", $spacer, $start_num+1, 
                         $ct, $num_posts_total );

    $nav_text .= "from <a href=\"$url\">rss feed</a> ";


    // print out "more posts" text
    if ( $ct < $num_posts_total ) {
        $nav_text .= sprintf(' <a href="?%s=%d%s">&gt;&gt;</a>', 
                             $start_num_name, $ct, $increment_text );
    }

    if ( !empty($nav_text) )
        echo "<p class=\"nav\" style=\"text-align:center\">" . $nav_text .
             "</p>";

    echo "</div>"; // close out blog


    if ( DEBUG_RSS ) {

        echo '<hr />';
        echo "ct: $ct, num_posts_total: $num_posts_total",
             " num_posts_visible: $num_posts_visible" ;
        echo  '<hr /><pre>';
        print_r( $rss );
        echo '</pre>';
    }

}

?>
